package org.study.swing.study02.demo;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.*;

// 继承自JFrame使得这个类成为一个窗体,可以对窗体的属性进行扩展并且可以定义自己需要的特殊操作方法
public class System extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;

    private Map<String, String> PersonInfo;
    public static Vector<Vector<String>> info = new Vector<>();
    private JLabel keyLab;
    private JButton searchBtn, createBtn, updateBtn, deleteBtn, exitBtn;
    public static JTable infoTable;
    private JTextField keyText;
    public static Vector<String> column;


    public System() {
        // 数组和链表的结合体,HashMap底层就是一个数组结构，数组中的每一项又是一个链表。新建一个HashMap的时候，就会初始化一个数组
        PersonInfo = new HashMap<>();
        // 设置字体，类型和大小；Front.PLAIN普通，Front.BLOD加粗，Front.ITALIC斜体
        Font font = new Font("宋体", Font.PLAIN, 15);
        JPanel pNorth = new JPanel();
        pNorth.setLayout(new FlowLayout(FlowLayout.LEFT));
        keyLab = new JLabel("请输入关键字：");
        // 搜索文本框
        keyText = new JTextField(10);

        // 创建系统功能按钮
        searchBtn = new JButton("搜索员工信息");
        createBtn = new JButton("新增员工信息");
        updateBtn = new JButton("修改员工信息");
        deleteBtn = new JButton("删除员工信息");
        exitBtn = new JButton("退出系统");

        // 设置字体大小
        keyLab.setFont(font);
        searchBtn.setFont(font);
        createBtn.setFont(font);
        updateBtn.setFont(font);
        deleteBtn.setFont(font);
        exitBtn.setFont(font);

        // 添加监听器
        searchBtn.addActionListener(this);
        createBtn.addActionListener(this);
        updateBtn.addActionListener(this);
        deleteBtn.addActionListener(this);
        exitBtn.addActionListener(this);

        // 在JPanel面板的上方加入搜索功能所需的一系列组件
        pNorth.add(keyLab);
        pNorth.add(keyText);
        pNorth.add(searchBtn);

        // 在JPanel面板下方加入系统功能组件
        JPanel pSouth = new JPanel();
        pSouth.add(createBtn);
        pSouth.add(updateBtn);
        pSouth.add(deleteBtn);
        pSouth.add(exitBtn);

        // 表格数据
        column = new Vector<>();
        column.add("编号");
        column.add("姓名");
        column.add("性别");
        column.add("电话");
        column.add("工号");
        column.add("薪资");
        column.add("部门");
        // 将数据存入数据库
        flashInfo();
        infoTable = new JTable(info, column);
        TableColumn column1 = infoTable.getColumnModel().getColumn(0);
        // 自适应
        column1.setPreferredWidth(30);

        TableColumn column3 = infoTable.getColumnModel().getColumn(2);
        // 自适应
        column3.setPreferredWidth(30);

        // 创建垂直滚动面板
        JScrollPane pCenter = new JScrollPane(infoTable);
        this.add(pNorth, "North");
        this.add(pCenter, "Center");
        this.add(pSouth, "South");

        this.setTitle("人事信息管理系统");
        this.setSize(800, 450);
        this.setVisible(true);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void flashInfo() {
        Connect dbconn = new Connect();
        Statement stmt = null;
        ResultSet rs = null;
        info.clear();
        try {
            // 创建一个 Statement 对象，封装 SQL 语句发送给数据库
            stmt = dbconn.getConnection().createStatement();
            // 下达命令执行查询语句并且存放在ResultSet对象中
            rs = stmt.executeQuery("select * from empinfo");
            while (rs.next()) {
                Vector<String> row = new Vector<String>();
                row.add(rs.getString(1));
                row.add(rs.getString(2));
                row.add(rs.getString(3));
                row.add(rs.getString(4));
                row.add(rs.getString(5));
                row.add(rs.getString(6));
                row.add(rs.getString(7));
                info.add(row);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 返回第一个选定行的索引
        int rowNum = infoTable.getSelectedRow();
        if (rowNum != -1) {
            PersonInfo = new HashMap<>();
            // 将值插入HasMap中
            // 返回表格row和column位置的单元格值
            PersonInfo.put("id", (String) infoTable.getValueAt(rowNum, 0));
            PersonInfo.put("name", (String) infoTable.getValueAt(rowNum, 1));
            PersonInfo.put("sex", (String) infoTable.getValueAt(rowNum, 2));
            PersonInfo.put("telephone", (String) infoTable.getValueAt(rowNum, 3));
            PersonInfo.put("worknumber", (String) infoTable.getValueAt(rowNum, 4));
            PersonInfo.put("salary", (String) infoTable.getValueAt(rowNum, 5));
            PersonInfo.put("did", (String) infoTable.getValueAt(rowNum, 6));
        }

        if (e.getSource() == searchBtn) {
            // 搜索
            String keyStr = keyText.getText();
            searchInfo(keyStr);
        } else if (e.getSource() == createBtn) {
            // 新建
            MyDialog InsertPane = new MyDialog("新建员工信息", new HashMap<>());
            InsertPane.setVisible(true);
        } else if (e.getSource() == updateBtn) {
            // 修改
            if (rowNum == -1) {
                // 提示弹窗
                JOptionPane.showMessageDialog(null, "请选择一条员工信息", "修改员工信息", JOptionPane.ERROR_MESSAGE);
                return;
            }
            MyDialog UpdatePane = new MyDialog("修改员工信息", PersonInfo);
            UpdatePane.setVisible(true);
        } else if (e.getSource() == deleteBtn) {
            // 删除
            if (rowNum == -1) {
                // 提示弹窗
                JOptionPane.showMessageDialog(null, "请选择一条员工信息", "删除员工信息", JOptionPane.ERROR_MESSAGE);
                return;
            }
            MyDialog DeletePane = new MyDialog("删除员工信息", PersonInfo);
            DeletePane.setVisible(true);
        } else if (e.getSource() == exitBtn) {
            // 退出
            this.setVisible(false);
        }
    }

    // 搜索
    protected void searchInfo(String key) {
        Connect dbconn = new Connect();
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = dbconn.getConnection().createStatement();
            String sql = "select * from empinfo where name like'%" + key + "%'";
            rs = stmt.executeQuery(sql);
            info.clear();
            while (rs.next()) {
                Vector<String> row = new Vector<String>();
                row.add(rs.getString(1));
                row.add(rs.getString(2));
                row.add(rs.getString(3));
                row.add(rs.getString(4));
                row.add(rs.getString(5));
                row.add(rs.getString(6));
                row.add(rs.getString(7));
                info.add(row);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
                if (rs != null) {
                    rs.close();
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }
            // 构造一个 DefaultTableModel，并通过将 data 和 columnNames 传递到 setDataVector 方法来初始化该表。
            DefaultTableModel model = new DefaultTableModel(System.info, System.column);
            // 数据绑定
            System.infoTable.setModel(model);
            TableColumn column1 = System.infoTable.getColumnModel().getColumn(0);
            column1.setMaxWidth(40);
            column1.setMinWidth(40);

            TableColumn column3 = System.infoTable.getColumnModel().getColumn(2);
            column3.setMaxWidth(40);
            column3.setMinWidth(40);
        }
    }

    public static void main(String[] args) {
        new System();
    }
}